package com.hk.player;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhoug.common.utils.TimeUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 正在录像时间显示组件
 * @Author 35574
 * @Date 2020/12/3
 * @Description
 */
public class RecordVideoTimeView extends FrameLayout {
    private volatile int recordTime = 0;//录像计时
    private TextView mRecordIcon;
    private TextView mRecordTime;
    private Handler mainHandler = new Handler(Looper.getMainLooper());

    public RecordVideoTimeView(@NonNull Context context) {
        this(context, null);
    }

    public RecordVideoTimeView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordVideoTimeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View content = LayoutInflater.from(context).inflate(R.layout.hk_player_record_video_time_view, this, false);
        addView(content);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.hk_player_RecordVideoTimeView, defStyleAttr, R.style.hk_player_RecordVideoView);
        Drawable background = a.getDrawable(R.styleable.hk_player_RecordVideoTimeView_android_background);
        int paddingStart = a.getDimensionPixelOffset(R.styleable.hk_player_RecordVideoTimeView_android_paddingStart, 0);
        int paddingEnd = a.getDimensionPixelOffset(R.styleable.hk_player_RecordVideoTimeView_android_paddingEnd, 0);
        int paddingTop = a.getDimensionPixelOffset(R.styleable.hk_player_RecordVideoTimeView_android_paddingTop, 0);
        int paddingBottom = a.getDimensionPixelOffset(R.styleable.hk_player_RecordVideoTimeView_android_paddingBottom, 0);
        a.recycle();
        setBackground(background);
        setPadding(paddingStart, paddingTop, paddingEnd, paddingBottom);

        findViews();

    }

    private void findViews() {
        mRecordIcon = findViewById(R.id.record_icon);
        mRecordTime = findViewById(R.id.record_time);
    }

    private Runnable timeRunnable = new Runnable() {
        @Override
        public void run() {
            recordTime++;
            if (mRecordIcon != null) {
                mRecordIcon.setVisibility(mRecordIcon.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
            }
            if (mRecordTime != null) {
                mRecordTime.setText(TimeUtils.formatSecond(recordTime));
            }
            mainHandler.postDelayed(this, 1000);

        }
    };

    /**
     * 开始录像时调用
     * 显示录像时间进度
     */
    public void start(){
        mainHandler.removeCallbacks(timeRunnable);
        recordTime = 0;
        mRecordTime.setText("00:00");
        setVisibility(VISIBLE);
        mainHandler.postDelayed(timeRunnable, 1000);
    }

    /**
     * 停止录像时调用
     * 隐藏录像时间进度
     */
    public void stop(){
        mainHandler.removeCallbacks(timeRunnable);
        recordTime = 0;
        setVisibility(View.GONE);
    }



}
